#!/bin/bash
#
# PB138 Task 2 Checker
# created by Tomáš Pitner, March 2011
# version 1.0, jaro 2011
#
# * Put this script into the same directory where the ZIP
#   with all solutions reside.
# * Put also the check-all-task2 here.
# * Make them both executable.
#
# * Run either this for single solution checking:
#   ./check-task2 Some_Student_UCO_blabla.zip [schema_file] [top_level_dir_in_zipped_project]
#
#   or to check all zipped in one file:
#   ./check-all-task2 uloha2.zip [schema_file] [top_level_dir_in_zipped_project]
#
# * The results for each individual solutions 
#   are always packed back into the ZIP 
#   file with the same name.
#
# * Overall results are present in:
#   
#   ok.txt       -- names of ZIPs that were completely OK
#   bad-zips.txt -- names of badly zipped ZIP archives
#   errors.txt   -- names of ZIPs with some validation errors,
#                   e.g. some points will be deducted...
#
# * System requirements: a Linux with xmllint, zip, bash
#

ZIP_FILE=$1

if [ "$2" = "" ]; then
SCHEMA=contacts.xsd
else
SCHEMA=$2
fi

if [ "$3" = "" ]; then
TOP=uloha2
else
TOP=$3
fi
DIR=$TOP/src/java

unzip $ZIP_FILE
if [ -d $DIR ]; then
cd $DIR

echo "" > success.txt
xmllint --noout --schema $SCHEMA kontakty.xml &>> success.txt
xmllint --noout --schema $SCHEMA zadne-kontakty.xml &>> success.txt

echo "" > failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-empty-adresy.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-cp1.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-cp2.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-popis.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-psc1.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-psc2.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-invalid-psc3.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-missing-city.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-missing-cp.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-missing-popis.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-missing-psc.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-adresa-missing-zeme.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-birthdate-invalid-day.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-birthdate-invalid-month.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-birthday-invalid-day2.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-duplicit-rc.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-emaily-missing-email.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-missing-birthdate.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-contains-letter-2.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-contains-letter.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-invalid-day.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-invalid-month.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-long-date.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-long-number.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-missing-slash.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-short-date-.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-rc-short-number.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefon-invalid-cislo1.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefon-invalid-cislo2.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefon-invalid-cislo3.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefon-missing-cislo.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefon-missing-popis.xml &>> failures.txt
xmllint --noout --schema $SCHEMA kontakty-telefony-empty-telefon.xml &>> failures.txt

echo "" > result.txt
echo "Should succeed 2 files:" >> result.txt
grep "validates" success.txt | cat -b >> result.txt
SUCCESSES=$(grep "validates" success.txt | wc -l)

echo "Should fail 33 files:" >> result.txt
grep "fails to validate" failures.txt | cat -b >> result.txt
FAILURES=$(grep "fails to validate" failures.txt | wc -l)

if [ "$SUCCESSES+$FAILURES" = "2+33" ]; then
	echo "OK" >> result.txt
	echo $ZIP_FILE >> ../../../ok.txt
else
	echo "Some errors" >> result.txt
	echo "$SUCCESSES+$FAILURES" >> result.txt
	echo "See success.txt and failures.txt for more details" >> result.txt
	echo $ZIP_FILE >> ../../../errors.txt
fi
cat result.txt

cd ../../..

zip -u -r -q $ZIP_FILE uloha2/
rm -rf uloha2

else
	echo "Badly zipped solution, $DIR does not exist"
	echo $ZIP_FILE >> bad-zips.txt
fi
